
function clickupload(){
	var file_uld=document.getElementById("fileupload").value;
	var type_chk=null;
	var type_file=null;
	if(document.getElementById("fileupload").value.length == 0){
		alert('ระบุ File ที่จะ Upload'); return false ;
	}
	type_chk=file_uld.split('.');
	type_file=type_chk[type_chk.length-1];
	
	if(type_file == 'jpg' || type_file =='png'){
		document.getElementById('upmsg').innerHTML = 'กำลัง Upload อยู่ รอซักครู่...' ;
		document.getElementById('btnUpload').value = "ส่งไปแล้วไม่ต้องคลิกซ้ำ";
		document.getElementById('btnUpload').disabled = true ;
		return true ;	
	}
	alert('กรุณา Upload File ที่มีนามสกุล jpg, png เท่านั้น');
	return false;
	
}

function del_onclick(var_len){
	var i, var_file;
	for(i=0;i<var_len;i++){
		var_file=document.getElementById("file_product_"+i).value;
		document.getElementById("img_del_"+i).onclick=function(){ del_select(var_file); }
	}
	return true;
}

function uploadok(pathfile){
	var file_chk=null;
	file_chk=pathfile.split('|');
	document.getElementById('upmsg').innerHTML=file_chk[0];
	var len_id=parseInt(file_chk[file_chk.length-1]);
	document.getElementById('photo_history').value=file_chk[file_chk.length-2];
	document.getElementById('fileupload').value ="";
	document.getElementById('btnUpload').value = "Attachment";
	document.getElementById('btnUpload').disabled = false;
	if(len_id > 0){
		setTimeout("del_onclick('"+len_id+"')", 100);
	}
	//document.getElementById('frmUpload').reset();
	return true ;
}

function fn_reset(var_element){
	switch(var_element){
		case "product" : {
			document.getElementById("ownership").value='';
			document.getElementById("product_name_th").value='';
			document.getElementById("product_name_en").value='';
			document.getElementById("property").value='';
			document.getElementById("ingredients").value='';
			document.getElementById("net_weight").value='';
			document.getElementById("product_price").value='';
			document.getElementById("instruction").value='';
			document.getElementById("remark").value='';
			document.getElementById("upmsg").value='';
			document.getElementById('frmUpload').reset();
		}break;
	}
}

function show_tr(tr_n_element){
	document.getElementById(tr_n_element).style.display="";
	document.getElementById("sh_"+tr_n_element).onclick=function(){ hide_tr(tr_n_element); }
	load_main();
}

function hide_tr(tr_n_element){
	document.getElementById(tr_n_element).style.display="none";
	document.getElementById("sh_"+tr_n_element).onclick=function(){ show_tr(tr_n_element); }
	load_main();
}

function fn_update(url_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	url='add_'+url_element+'.php';

}

function fn_add(url_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	url='add_'+url_element+'.php';

switch(url_element){
	case "product" :{

		value_url='product_id='+encodeURIComponent(document.getElementById("product_id").value);
		value_url+='&ownership='+encodeURIComponent(document.getElementById("ownership").value);
		value_url+='&product_name_th='+encodeURIComponent(document.getElementById("product_name_th").value);
		value_url+='&product_name_en='+encodeURIComponent(document.getElementById("product_name_en").value);
		value_url+='&property='+encodeURIComponent(document.getElementById("property").value);
		value_url+='&ingredients='+encodeURIComponent(document.getElementById("ingredients").value);
		value_url+='&net_weight='+encodeURIComponent(document.getElementById("net_weight").value);
		value_url+='&product_price='+encodeURIComponent(document.getElementById("product_price").value);
		value_url+='&instruction='+encodeURIComponent(document.getElementById("instruction").value);
		value_url+='&remark='+encodeURIComponent(document.getElementById("remark").value);
		value_url+='&status_import='+parseInt(document.getElementById("status_import").value);
		var obj_sub_group_id=document.getElementById("sub_group_id");
		var sub_group_id=obj_sub_group_id.options[obj_sub_group_id.selectedIndex].value;
		//var sub_group_id=document.getElementById("sub_group_id").value;
		/*if(document.getElementById("sub_group_id").value == '0'){
			sub_group_id=null;
		}*/
		value_url+='&sub_group_id='+sub_group_id;
		value_url+='&photo_history='+encodeURIComponent(document.getElementById("photo_history").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;
	case "sub_product" :{
		value_url='sub_product_id='+encodeURIComponent(document.getElementById("sub_product_id").value);
		value_url+='&sub_product_desc='+encodeURIComponent(document.getElementById("sub_product_desc").value);
		var obj_group_product_id=document.getElementById("group_product_id");
		var group_product_id=obj_group_product_id.options[obj_group_product_id.selectedIndex].value;
		value_url+='&group_product_id='+encodeURIComponent(group_product_id);
		value_url+='&hash='+new Date().getTime()+Math.random();

		//return alert('add sub product');
	}break;
}

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
								if(data[0].success == "false"){
									alert(data[0].comment);
									return 0;
								}
								goto_page(url_element);
								/*document.getElementById("data_"+url_element).innerHTML = data[0].result_data;
								if(url_element == "product"){
									document.getElementById("product_id").value = data[0].product_id_new;
								}else if(url_element == "sub_product"){
									document.getElementById("sub_product_id").value = data[0].sub_product_no;
								}
								fn_reset(url_element);
								load_main();*/
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

function fn_edit(url_element, id_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	fn_delete_img();

	if(url_element != "product") return alert('Edit ID='+id_element);
	url='mod_menu.php';
	value_url='case_select='+url_element+'&mode=edit&id='+id_element;
	value_url+='&hash='+new Date().getTime()+Math.random();
	
	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							document.getElementById("menu_"+url_element).innerHTML=data[0].result_data;
							//return alert('Edit ID='+id_element);
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);

	//return alert('Edit ID='+id_element);
}

function fn_update(url_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	url='update_'+url_element+'.php';

switch(url_element){
	case "product" :{

		value_url='product_id='+encodeURIComponent(document.getElementById("product_id").value);
		value_url+='&ownership='+encodeURIComponent(document.getElementById("ownership").value);
		value_url+='&product_name_th='+encodeURIComponent(document.getElementById("product_name_th").value);
		value_url+='&product_name_en='+encodeURIComponent(document.getElementById("product_name_en").value);
		value_url+='&property='+encodeURIComponent(document.getElementById("property").value);
		value_url+='&ingredients='+encodeURIComponent(document.getElementById("ingredients").value);
		value_url+='&net_weight='+encodeURIComponent(document.getElementById("net_weight").value);
		value_url+='&product_price='+encodeURIComponent(document.getElementById("product_price").value);
		value_url+='&instruction='+encodeURIComponent(document.getElementById("instruction").value);
		value_url+='&remark='+encodeURIComponent(document.getElementById("remark").value);
		value_url+='&status_import='+parseInt(document.getElementById("status_import").value);
		var obj_sub_group_id=document.getElementById("sub_group_id");
		var sub_group_id=obj_sub_group_id.options[obj_sub_group_id.selectedIndex].value;
		value_url+='&sub_group_id='+sub_group_id;
		value_url+='&photo_history='+encodeURIComponent(document.getElementById("photo_history").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;
	case "sub_product" :{
		value_url='sub_product_id='+encodeURIComponent(document.getElementById("sub_product_id").value);
		value_url+='&sub_product_desc='+encodeURIComponent(document.getElementById("sub_product_desc").value);
		var obj_group_product_id=document.getElementById("group_product_id");
		var group_product_id=obj_group_product_id.options[obj_group_product_id.selectedIndex].value;
		value_url+='&group_product_id='+encodeURIComponent(group_product_id);
		value_url+='&hash='+new Date().getTime()+Math.random();

		//return alert('add sub product');
	}break;
}

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
								if(data[0].success == "false"){
									alert(data[0].comment);
									return 0;
								}
								goto_page(url_element);
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

function fn_cancel(url_element){
	goto_page(url_element);
}

function del_select(element_var){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	url='unlink_image.php';
	value_url='file_name='+encodeURIComponent(element_var);

	if(document.getElementById("photo_history")){
		if(document.getElementById("photo_history").value != ""){
			var path_image=encodeURIComponent(document.getElementById("photo_history").value);
			value_url+='&path_image='+path_image;
		}
	}

	value_url+='&hash='+new Date().getTime()+Math.random();
	
			req.open("POST", url, true);
				req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							if(case_del == "unlink"){
								document.getElementById("photo_history").value=data[0].photo_history;
								document.getElementById('upmsg').innerHTML=data[0].result_data;
							}
						}
					}
				};
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			req.send(value_url);
}

function fn_delete_img(){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;

	if(document.getElementById("photo_history")){
		if(document.getElementById("photo_history").value != ""){
			url='unlink_image.php';
			value_url='path_image='+encodeURIComponent(document.getElementById('photo_history').value);
			value_url+='&hash='+new Date().getTime()+Math.random();

			req.open("POST", url, true);
				req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
						}
					}
				};
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			req.send(value_url);
		}
	}
}

function fn_delete(url_element, id_element){
	return alert('Delete ID='+id_element);
}

